package com.oa.demo.auth.service.impl;

import com.oa.demo.auth.entity.TUser;
import com.oa.demo.auth.mapper.TUserMapper;
import com.oa.demo.auth.service.ITUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.oa.demo.common.exception.BaseAuthException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kongbox
 * @since 2020-11-27
 */
@Slf4j
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {

    @Override
    public Page<TUser> listTUsersByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询tUser: page = {} pageSize = {} factor = {}", page, pageSize, factor);
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<TUser>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<TUser> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询tUser完毕: 结果数 = {} ", result.getRecords().size());
        return result;
    }

    @Override
    public TUser getTUserById(int id) {
        log.info("正在查询tUser中id为{}的数据", id);
        TUser tUser = super.getById(id);
        log.info("查询id为{}的tUser{}", id, (null == tUser ? "无结果" : "成功"));
        return tUser;
    }

    @Override
    public int insertTUser(TUser tUser) throws BaseAuthException {
        log.info("正在插入tUser");
        if (super.save(tUser)) {
            log.info("插入tUser成功,id为{}", tUser.getId());
            return tUser.getId();
        } else {
            log.error("插入tUser失败");
            throw new BaseAuthException("添加失败");
        }
    }

    @Override
    public int deleteTUserById(int id) throws BaseAuthException {
        log.info("正在删除id为{}的tUser", id);
        if (super.removeById(id)) {
            log.info("删除id为{}的tUser成功", id);
            return id;
        } else {
            log.error("删除id为{}的tUser失败", id);
            throw new BaseAuthException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateTUser(TUser tUser) throws BaseAuthException {
        log.info("正在更新id为{}的tUser", tUser.getId());
        if (super.updateById(tUser)) {
            log.info("更新d为{}的tUser成功", tUser.getId());
            return tUser.getId();
        } else {
            log.error("更新id为{}的tUser失败", tUser.getId());
            throw new BaseAuthException("更新失败[id=" + tUser.getId() + "]");
        }
    }

}
